﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UofCASE.AgileProductLinerDSL.Nant.Tasks.Models;
using System.IO;
using APLDTests.NantTests.Mocks;

namespace APLDTests.NantTests
{
    [TestClass]
    public class EmptyTestSet
    {
        Feature feature;
        Platform platform;
        TestSet set;

        [TestInitialize]
        public void setup()
        {
            feature = new Feature("Bootup");
            platform = new Platform("Windows");
            set = new TestSet(platform, feature);
            
        }

        [TestMethod]
        public void IsPassing()
        {
            Assert.IsTrue(set.Passed);
        }

        [TestMethod]
        public void KeySeachReturnsNull()
        {
            ITestKey key = new UnMatchableKey();
            Assert.IsNull(set.Find(key));
        }

        [TestMethod]
        public void IncludesAddedTests()
        {
            ITest test = new DummyTest(true);
            set.AddTest(test);
            Assert.IsTrue(set.Contains(test));
        }
    }

    [TestClass]
    public class TestSetWithOneElement
    {
        Feature feature;
        Platform platform;
        TestSet set;

        [TestInitialize]
        public void setup()
        {
            feature = new Feature("Bootup");
            platform = new Platform("Windows");
            set = new TestSet(platform, feature);
            set.AddTest(new DummyTest(true));
        }

        [TestMethod]
        public void IsPassing()
        {
            Assert.IsTrue(set.Passed);
        }

        [TestMethod]
        public void FailsIfFailingTestIsAdded()
        {
            set.AddTest(new DummyTest(false));
            Assert.IsFalse(set.Passed);
        }
    }

    [TestClass]
    public class TestSetWithSeveralElements
    {
        Feature feature;
        Platform platform;
        TestSet set;

        [TestInitialize]
        public void setup()
        {
            feature = new Feature("Bootup");
            platform = new Platform("Windows");
            set = new TestSet(platform, feature);
            set.AddTest(new DummyTest(true));
            set.AddTest(new DummyTest(true));
            set.AddTest(new DummyTest(false));
        }

        [TestMethod]
        public void KeySeachReturnsTestLookedFor()
        {
            ITest test = set.ElementAt(1);
            ITestKey key = test.Key;
            Assert.AreSame(test, set.Find(key));
        }

        [TestMethod]
        public void IsEnumerable()
        {
            IEnumerator<ITest> tests = set.GetEnumerator();
            Assert.IsTrue(tests.MoveNext());
            Assert.IsTrue(tests.MoveNext());
            Assert.IsTrue(tests.MoveNext());
            Assert.IsFalse(tests.MoveNext());
        }
    
    }

    [TestClass]
    public class TestSetConstruction
    {
        [TestMethod]
        public void ConnectsBacklinksInParents()
        {
            Feature f = new Feature("Feature");
            Platform p = new Platform("Platform");
            TestSet set = new TestSet(p, f);
            Assert.AreSame(set, f.TestsFor(p));
            Assert.AreSame(set, p.TestsFor(f));
        }
    }
}
